package com.wanqing.community.tag;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Logger;

import com.wanqing.community.bean.MenuOperateBean;
import com.wanqing.community.util.KeyConstant;

public class ToolBarTag extends TagSupport{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static Logger log = Logger.getLogger(ToolBarTag.class);

	//菜单ID
	private String menuId;
	
	private boolean showButton = true;
	
	public String getMenuId() {
		return menuId;
	}

	public void setMenuId(String menuId) {
		this.menuId = menuId;
	}

	public int doStartTag(){
		try{
			String result = getAtagHtml();
			pageContext.getOut().println(result);
		}catch (Exception e) {
			e.printStackTrace();
			log.error("产生操作工具条异常："+e.toString());
		}
		return EVAL_PAGE;
	}

	private String getAtagHtml() {
		if(!showButton){
			return "<div></div>";
		}
		StringBuilder sb = new StringBuilder();	
		List<MenuOperateBean> list = getMenuList();
		sb.append("<div>\n");		
		if(null!=list && !list.isEmpty()){
			for (MenuOperateBean mo : list) {
				String opername = mo.getOperatename();
				String opercode = mo.getOperatecode();
				String iconCls = mo.getOperateIconCls();				
				sb.append("<a href=\"javascript:void(0);\" class=\"easyui-linkbutton\" iconCls=\""+iconCls+"\" id=\""+opercode+"\" plain=\"true\">"+opername+"</a>\n");
			}
		}		
		sb.append("</div>");		
		return sb.toString();
	}
	
	@SuppressWarnings("unchecked")
	private List<MenuOperateBean> getMenuList(){
		HttpSession session = this.pageContext.getSession();
		Map<String, List<MenuOperateBean>> map = (Map<String, List<MenuOperateBean>>) session.getAttribute(KeyConstant.FUNC_MENU_FUNTREE);
	    if(null!=map && !map.isEmpty()){
	    	return map.get(menuId);
	    }
	    return null;
	}

	public boolean isShowButton() {
		return showButton;
	}

	public void setShowButton(boolean showButton) {
		this.showButton = showButton;
	}

}
